package com.allen.app.zdemo.mq;

import cn.hutool.extra.spring.SpringUtil;
import com.allen.app.common.utils.Jackson;
import com.allen.app.zdemo.mapper.RabbitmqDatasMapper;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;


@UtilityClass
@Slf4j
public class RabbitMqHelper {

    private final static RabbitTemplate RABBIT;
    private final static RabbitmqDatasMapper MAPPER;

    static{
        RABBIT = SpringUtil.getBean(RabbitTemplate.class);
        MAPPER = SpringUtil.getBean(RabbitmqDatasMapper.class);
    }

    /**
     * 发送不同延迟时间的延迟队列.
     *
     * @param exchange exchange
     * @param routingKey routingKey
     * @param messageJson messageJson
     * @param delayTime delayTime
     * @param businessId businessId
     * @return void
     * @author Limiaojun
     * @date 2020/8/5 0005 11:58
     */
    public void convertAndSendPlugs(String exchange, String routingKey, Object messageJson, Long delayTime,
        Object businessId) {
        messageJson = !(messageJson instanceof String) ? Jackson.json(messageJson) : messageJson;

        RABBIT.convertAndSend(exchange,
            routingKey, messageJson,
            message -> {
                message.getMessageProperties().setDelay(delayTime.intValue());
                return message;
            });

        MAPPER.create(new RabbitmqDatas(exchange, routingKey, messageJson.toString(), businessId, delayTime));
    }

    /**
     * Description:根据exchangeName、queueName、businessId删除或修改rabbitmq队列消息
     *
     * @param rabbitmqDatas rabbitmq队列消息
     * @param errFlag true 修改, false 删除
     * @return int
     * @author Limiaojun
     * @date 2020/03/12 12:59:55
     */
    public int deleteOrUpdateByUnionCondition(RabbitmqDatas rabbitmqDatas,boolean errFlag){
        if(errFlag){
            rabbitmqDatas.setStatus("FAIL");
            return MAPPER.updateByUnionCondition(rabbitmqDatas);
        }else{
            return MAPPER.deleteByUnionCondition(rabbitmqDatas);
        }
    }

    /**
     * 发送普通延时队列(延时时间一致).
     *
     * @param exchange exchange
     * @param routingKey routingKey
     * @param messageJson messageJson
     * @param delayTime delayTime
     * @return void
     * @author Limiaojun
     * @date 2021/07/12 12:25
     */
    public void convertAndSend(String exchange, String routingKey, Object messageJson, Long delayTime) {
        messageJson = !(messageJson instanceof String) ? Jackson.json(messageJson) : messageJson;
        RABBIT.convertAndSend(exchange,
            routingKey, messageJson,
            message -> {
                message.getMessageProperties().setExpiration(delayTime.toString());
                return message;
            });
    }

    /**
     * 普通队列.
     * @param exchange  exchange
     * @param routingKey  routingKey
     * @param messageJson  messageJson
     * @return void
     * @author Limiaojun
     * @date 2021/07/12 14:52
     */
    public void convertAndSend(String exchange, String routingKey, Object messageJson) {
        messageJson = !(messageJson instanceof String) ? Jackson.json(messageJson) : messageJson;
        RABBIT.convertAndSend(exchange,routingKey, messageJson);
    }
}

